package com.ruoyi.web.xxj.contoller;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.xxj.domain.ClassOrder;
import com.ruoyi.web.xxj.domain.ProductOrder;
import com.ruoyi.web.xxj.domain.RefundInfo;
import com.ruoyi.web.xxj.domain.pojo.RefundInfoReq;
import com.ruoyi.web.xxj.domain.vo.ClassOrderVo;
import com.ruoyi.web.xxj.domain.vo.RefundInfoVo;
import com.ruoyi.web.xxj.service.IClassOrderService;
import com.ruoyi.web.xxj.service.IProductOrderService;
import com.ruoyi.web.xxj.service.IRefundInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 退款记录Controller
 *
 * @author ruoyi
 * @date 2024-02-19
 */
@Api(tags = "退款信息管理")
@RestController
@RequestMapping("/xxj/refundInfo")
public class RefundInfoController extends BaseController {
    @Autowired
    private IRefundInfoService refundInfoService;
    @Autowired
    private IClassOrderService classOrderService;
    @Autowired
    private IProductOrderService productOrderService;

    /**
     * 查询退款记录列表
     */
    @ApiOperation("查询退款记录列表")
    @GetMapping("/list")
    public TableDataInfo list(RefundInfoReq refundInfoReq) {
        RefundInfo refundInfo = null;
        if (Objects.isNull(refundInfoReq) || (StringUtils.isEmpty(refundInfoReq.getOrderNo()) && StringUtils.isEmpty(refundInfoReq.getWxpayTradeNo()))) {
            refundInfo = new RefundInfo();
        } else {
            if (refundInfoReq.getType().equals("class")) {
                List<ClassOrderVo> orders = classOrderService.selectClassOrderList(new ClassOrder(refundInfoReq.getOrderNo(), refundInfoReq.getWxpayTradeNo()));
                if (CollectionUtils.isEmpty(orders)) {
                    return getDataTable(Collections.EMPTY_LIST);
                }
                refundInfo = new RefundInfo(orders.get(0).getId(), null);
            } else {
                List<ProductOrder> orders = productOrderService.selectProductOrderList(new ProductOrder(refundInfoReq.getOrderNo(), refundInfoReq.getWxpayTradeNo()));
                if (CollectionUtils.isEmpty(orders)) {
                    return getDataTable(Collections.EMPTY_LIST);
                }
                refundInfo = new RefundInfo(null, orders.get(0).getId());
            }
        }
        startPage();
        List<RefundInfoVo> list = refundInfoService.selectRefundInfoList(refundInfo);
        return getDataTable(list);
    }

    /**
     * 导出退款记录列表
     */
    @ApiOperation("导出退款记录列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, RefundInfo refundInfo) {
        List<RefundInfoVo> list = refundInfoService.selectRefundInfoList(refundInfo);
        ExcelUtil<RefundInfoVo> util = new ExcelUtil<RefundInfoVo>(RefundInfoVo.class);
        util.exportExcel(response, list, "退款记录数据");
    }

    /**
     * 获取退款记录详细信息
     */
    @ApiOperation("获取退款记录详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(refundInfoService.selectRefundInfoById(id));
    }

    /**
     * 新增退款记录
     */
    @PostMapping
    public AjaxResult add(@RequestBody RefundInfo refundInfo) {
        return toAjax(refundInfoService.insertRefundInfo(refundInfo));
    }

    /**
     * 修改退款记录
     */
    @ApiOperation("修改退款记录")
    @PutMapping
    public AjaxResult edit(@RequestBody RefundInfo refundInfo) {
        return toAjax(refundInfoService.updateRefundInfo(refundInfo));
    }

    /**
     * 删除退款记录
     */
    @ApiOperation("删除退款记录")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(refundInfoService.deleteRefundInfoByIds(ids));
    }
}
